{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d42f23fb",
   "metadata": {},
   "source": [
    "* 2022/09/13/week04\n",
    "* python课堂笔记"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ae95995c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 5)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 上周回顾（range()）\n",
    "range(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "68ba7da5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# for + range 决定循环次数\n",
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dbe216c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(1, 5)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(1,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e3a99040",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b46da5d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10,3):\n",
    "    print(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fb27e6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "## star? stop? step?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f95613b",
   "metadata": {},
   "source": [
    "## 本周内容\n",
    "\n",
    "* type()：检查类型\n",
    "\n",
    "\n",
    "> 1. 数值\n",
    ">> 1.int\n",
    ">> 2.float\n",
    ">> 3.complex\n",
    "\n",
    "> 2. 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "268741a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "## Number(数值)\n",
    "\n",
    "a = 9\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6f5c2894",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11.5 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "## float\n",
    "b = 11.5\n",
    "print(b,type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "83f9b928",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5+3j) <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "## complex(虚数，复数)\n",
    "c = 5 + 3j\n",
    "print(c,type(c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82c36fab",
   "metadata": {},
   "source": [
    "## 2.字符串（string）\n",
    "\n",
    "> 1. 关于字符串的基本内容\n",
    "> 2. 关于字符串的方法及函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d55dd2b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 1.字符串的基本内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "996d5ab7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alex <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "name = \"Alex\"\n",
    "print(name,type(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9dea1bbd",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (3314369162.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [14]\u001b[1;36m\u001b[0m\n\u001b[1;33m    'spam eggs'  # single quotes\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "### string\n",
    " 'spam eggs'  # single quotes\n",
    "'spam eggs'  \n",
    "\n",
    " 'doesn\\'t'  # use \\' to escape the single quote...\n",
    "\"doesn't\"  \n",
    "\n",
    " \"doesn't\"  # ...or use double quotes instead\n",
    "\"doesn't\"  \n",
    "\n",
    " '\"Yes,\" they said.'\n",
    "'\"Yes,\" they said.'  \n",
    "\n",
    " \"\\\"Yes,\\\" they said.\"\n",
    "'\"Yes,\" they said.'  \n",
    "\n",
    " '\"Isn\\'t,\" they said.'\n",
    "'\"Isn\\'t,\" they said.'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df9f7bf8",
   "metadata": {},
   "source": [
    "### 2.2 [关于字符串的方法及函数 ](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str)\n",
    "\n",
    "1. single quotes: 'allows embedded \"double\" quotes'\n",
    "\n",
    "2. Double quotes: \"allows embedded 'single' quotes\"\n",
    "\n",
    "3. Triple quoted: '''Three single quotes''', \"\"\"Three double quotes\"\"\"\n",
    "\n",
    "4. in检查成员对象\n",
    "\n",
    "5. 字符串的拼接\n",
    "\n",
    "6. 字符串的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "617f7616",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广州南方学院坐落于广州市从化温泉镇\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 关于有序性\n",
    "title = \"广州南方学院坐落于广州市从化温泉镇\"\n",
    "print(title)\n",
    "title[0] # 取单一值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "41166101",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(title)# 检查文本的总长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "21fd85c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'镇'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f1cd94e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[0:6] # 取一定范围的值 这种方法叫做切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8c9f5e60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10f178e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "## enumerate 枚举，既可以获取到这个内容item又可以获取到索引值index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "4b37fab0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 广\n",
      "1 州\n",
      "2 南\n",
      "3 方\n",
      "4 学\n",
      "5 院\n",
      "6 坐\n",
      "7 落\n",
      "8 于\n",
      "9 广\n",
      "10 州\n",
      "11 市\n",
      "12 从\n",
      "13 化\n",
      "14 温\n",
      "15 泉\n",
      "16 镇\n"
     ]
    }
   ],
   "source": [
    "## 利用for循环 遍历文本信息\n",
    "for i,j in enumerate(title):\n",
    "    print(i,j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b030cbaf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4cc7790f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广州这个词出现在这段文本中\n",
      "广州这个词出现在这段文本中\n"
     ]
    }
   ],
   "source": [
    "# 思考:如何判断一段文本中是否存在“广州”，例如文本title中的内容\n",
    "for i,j in enumerate(title):\n",
    "    if j == \"广\":\n",
    "        if title[i+1] == \"州\":\n",
    "           print(\"广州这个词出现在这段文本中\")\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7dc73628",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## page29 in检查成员对象\n",
    "\"广州\" in title"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "768ab61b",
   "metadata": {},
   "source": [
    "* 字符串的拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "29de3caa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好,Alex\n"
     ]
    }
   ],
   "source": [
    "print(\"你好\"+ \",\" + \"Alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "12b9ec70",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [38]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m你好\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAlex\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "# 字符串不可以和数字进行连接\n",
    "print(\"你好\"+ 1 + \"Alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c046a1e1",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for /: 'str' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [39]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 字符串不可以用来运算\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m3\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m2\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'str' and 'str'"
     ]
    }
   ],
   "source": [
    "# 字符串不可以用来运算\n",
    "\"3\"/\"2\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "633e3178",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"marry\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "bd58f186",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "d6ed710b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.count(\"r\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b71892e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
